home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sound / c140.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  8KB  |  368 lines

  1. /*
  2. C140.c
  3.  
  4. Simulator based on AMUSE sources.
  5. The C140 sound chip is used by Namco SystemII, System21
  6. This chip controls 24 channels of PCM.
  7. 16 bytes are associated with each channel.
  8. Channels can be 8 bit signed PCM, or 12 bit signed PCM.
  9.  
  10. Timer behavior is not yet handled.
  11.  
  12. Unmapped registers:
  13.     0x1f8:timer interval?    (Nx0.1 ms)
  14.     0x1fa:irq ack? timer restart?
  15.     0x1fe:timer switch?(0:off 1:on)
  16. */
  17.  
  18. #include <math.h>
  19. #include "driver.h"
  20.  
  21. #define MAX_VOICE 24
  22.  
  23. struct voice_registers
  24. {
  25.     UINT8 volume_right;
  26.     UINT8 volume_left;
  27.     UINT8 frequency_msb;
  28.     UINT8 frequency_lsb;
  29.     UINT8 bank;
  30.     UINT8 mode;
  31.     UINT8 start_msb;
  32.     UINT8 start_lsb;
  33.     UINT8 end_msb;
  34.     UINT8 end_lsb;
  35.     UINT8 loop_msb;
  36.     UINT8 loop_lsb;
  37.     UINT8 reserved[4];
  38. };
  39.  
  40. static int sample_rate;
  41. static int stream;
  42.  
  43. /* internal buffers */
  44. static short *mixer_buffer_left;
  45. static short *mixer_buffer_right;
  46.  
  47. static long baserate;
  48. static void *pRom;
  49. static UINT8 REG[0x200];
  50.  
  51. typedef struct
  52. {
  53.     long    ptoffset;
  54.     long    pos;
  55.     long    key;
  56.     //--work
  57.     long    lastdt;
  58.     long    prevdt;
  59.     long    dltdt;
  60.     //--reg
  61.     long    rvol;
  62.     long    lvol;
  63.     long    frequency;
  64.     long    bank;
  65.     long    mode;
  66.  
  67.     long    sample_start;
  68.     long    sample_end;
  69.     long    sample_loop;
  70. } VOICE;
  71.  
  72. VOICE voi[MAX_VOICE];
  73.  
  74. static void init_voice( VOICE *v )
  75. {
  76.     v->key=0;
  77.     v->ptoffset=0;
  78.     v->rvol=0;
  79.     v->lvol=0;
  80.     v->frequency=0;
  81.     v->bank=0;
  82.     v->mode=0;
  83.     v->sample_start=0;
  84.     v->sample_end=0;
  85.     v->sample_loop=0;
  86. }
  87.  
  88. READ_HANDLER( C140_r )
  89. {
  90.     offset&=0x1ff;
  91.     return REG[offset];
  92. }
  93.  
  94. static long find_sample( long adrs, long bank)
  95. {
  96.     adrs=(bank<<16)+adrs;
  97.     /*return adrs&0xfffff;*/
  98.     return ((adrs&0x200000)>>2)|(adrs&0x7ffff);        /* 991104.CAB */
  99. }
  100.  
  101. WRITE_HANDLER( C140_w )
  102. {
  103.     stream_update(stream, 0);
  104.  
  105.     offset&=0x1ff;
  106.  
  107.     REG[offset]=data;
  108.     if( offset<0x180 )
  109.     {
  110.         VOICE *v = &voi[offset>>4];
  111.         if( (offset&0xf)==0x5 )
  112.         {
  113.             if( data&0x80 )
  114.             {
  115.                 const struct voice_registers *vreg = (struct voice_registers *) ®[offset&0x1f0];
  116.                 v->key=1;
  117.                 v->ptoffset=0;
  118.                 v->pos=0;
  119.                 v->lastdt=0;
  120.                 v->prevdt=0;
  121.                 v->dltdt=0;
  122.                 v->bank = vreg->bank;
  123.                 v->mode = data;
  124.                 v->sample_loop = vreg->loop_msb*256 + vreg->loop_lsb;
  125.                 v->sample_start = vreg->start_msb*256 + vreg->start_lsb;
  126.                 v->sample_end = vreg->end_msb*256 + vreg->end_lsb;
  127.             }
  128.             else
  129.             {
  130.                 v->key=0;
  131.             }
  132.         }
  133.     }
  134. }
  135.  
  136. /* 991112.CAB */
  137. INLINE int limit(int in)
  138. {
  139.     if(in>0x7fff)        return 0x7fff;
  140.     else if(in<-0x8000)    return -0x8000;
  141.     return in;
  142. }
  143.  
  144. static void update_stereo(int ch, INT16 **buffer, int length)
  145. {
  146.     long    i,j;
  147.     long    rvol,lvol;
  148.     long    dt;
  149.     long    sdt;
  150.     long    st,ed,sz;
  151.  
  152.     signed char    *pSampleData;
  153.     long    frequency,delta,offset,pos;
  154.     long    cnt;
  155.     long    lastdt,prevdt,dltdt;
  156.     float    pbase=(float)baserate*2.0 / (float)sample_rate;
  157.  
  158.     short *lmix, *rmix;
  159.  
  160.     if(length>sample_rate) length=sample_rate;
  161.  
  162.     /* zap the contents of the mixer buffer */
  163.     memset(mixer_buffer_left, 0, length * sizeof(INT16));
  164.     memset(mixer_buffer_right, 0, length * sizeof(INT16));
  165.  
  166.     //--- audio update
  167.     for( i=0;i<MAX_VOICE;i++ )
  168.     {
  169.         VOICE *v = &voi[i];
  170.         const struct voice_registers *vreg = (struct voice_registers *)®[i*16];
  171.  
  172.         if( v->key )
  173.         {
  174.             frequency= vreg->frequency_msb*256 + vreg->frequency_lsb;
  175.  
  176.             /* Abort voice if no frequency value set */
  177.             if(frequency==0) continue;
  178.  
  179.             /* Delta =  frequency * ((8Mhz/374)*2 / sample rate) */
  180.             delta=(long)((float)frequency * pbase);
  181.  
  182.             /* Calculate left/right channel volumes */
  183.             lvol=(vreg->volume_left*32)/MAX_VOICE; //32ch -> 24ch
  184.             rvol=(vreg->volume_right*32)/MAX_VOICE;
  185.  
  186.             /* Set mixer buffer base pointers */
  187.             lmix = mixer_buffer_left;
  188.             rmix = mixer_buffer_right;
  189.  
  190.             /* Retrieve sample start/end and calculate size */
  191.             st=v->sample_start;
  192.             ed=v->sample_end;
  193.             sz=ed-st;
  194.  
  195.             /* Retrieve base pointer to the sample data */
  196.             pSampleData=(signed char*)((unsigned long)pRom + find_sample(st,v->bank));
  197.  
  198.             /* Fetch back previous data pointers */
  199.             offset=v->ptoffset;
  200.             pos=v->pos;
  201.             lastdt=v->lastdt;
  202.             prevdt=v->prevdt;
  203.             dltdt=v->dltdt;
  204.  
  205.             /* Switch on data type */
  206.             if(v->mode&8)
  207.             {
  208.                 //Mu-law?? pcm(but still wrong...)
  209.                 lvol *= 2.4;
  210.                 rvol *= 2.4;
  211.  
  212.                 /* Loop for enough to fill sample buffer as requested */
  213.                 for(j=0;j<length;j++)
  214.                 {
  215.  
  216.                     offset += delta;
  217.                     cnt = (offset>>16)&0x7fff;
  218.                     offset &= 0xffff;
  219.                     pos+=cnt;
  220.                     //for(;cnt>0;cnt--)
  221.                     {
  222.                         /* Check for the end of the sample */
  223.                         if(pos >= sz)
  224.                         {
  225.                             /* Check if its a looping sample, either stop or loop */
  226.                             if(v->mode&0x10)
  227.                             {
  228.                                 pos = (v->sample_loop - st);
  229.                             }
  230.                             else
  231.                             {
  232.                                 v->key=0;
  233.                                 break;
  234.                             }
  235.                         }
  236.  
  237.                         /* Read the chosen sample byte */
  238.                         dt=pSampleData[pos];
  239.  
  240.                         /* ???????? */
  241.                         sdt=dt>>3;                //signed
  242.                         sdt = (sdt<<(dt&7));
  243.  
  244.                         prevdt=lastdt;
  245.                         lastdt=sdt;                //12bits
  246.                         dltdt=(lastdt - prevdt);
  247.                     }
  248.  
  249.                     /* Caclulate the sample value */
  250.                     if( 1 )//bInterpolate)
  251.                         dt=((dltdt*offset)>>16)+prevdt;
  252.                     else
  253.                         dt=lastdt;
  254.  
  255.                     /* Write the data to the sample buffers */
  256.                     *lmix++ +=(dt*lvol)>>(5+4+1);    //include 2.0xgain
  257.                     *rmix++ +=(dt*rvol)>>(5+4+1);
  258.                 }
  259.             }
  260.             else
  261.             {
  262.                 /* linear 8bit signed PCM */
  263.  
  264.                 for(j=0;j<length;j++)
  265.                 {
  266.                     offset += delta;
  267.                     cnt = (offset>>16)&0x7fff;
  268.                     offset &= 0xffff;
  269.                     pos += cnt;
  270.                     /* Check for the end of the sample */
  271.                     if(pos >= sz)
  272.                     {
  273.                         /* Check if its a looping sample, either stop or loop */
  274.                         if( v->mode&0x10 )
  275.                         {
  276.                             pos = (v->sample_loop - st);
  277.                         }
  278.                         else
  279.                         {
  280.                             v->key=0;
  281.                             break;
  282.                         }
  283.                     }
  284.  
  285.                     if( cnt )
  286.                     {
  287.                         prevdt=lastdt;
  288.                         lastdt=pSampleData[pos];
  289.                         dltdt=(lastdt - prevdt);
  290.                     }
  291.  
  292.                     /* Caclulate the sample value */
  293.                     if(1 )//bInterpolate)
  294.                         dt=((dltdt*offset)>>16)+prevdt;
  295.                     else
  296.                         dt=lastdt;
  297.  
  298.                     /* Write the data to the sample buffers */
  299.                     *lmix++ +=(dt*lvol)>>5;
  300.                     *rmix++ +=(dt*rvol)>>5;
  301.                 }
  302.             }
  303.  
  304.             /* Save positional data for next callback */
  305.             v->ptoffset=offset;
  306.             v->pos=pos;
  307.             v->lastdt=lastdt;
  308.             v->prevdt=prevdt;
  309.             v->dltdt=dltdt;
  310.         }
  311.         //mute>>=1;
  312.     }
  313.  
  314.     /* render to MAME's stream buffer */
  315.     lmix = mixer_buffer_left;
  316.     rmix = mixer_buffer_right;
  317.     {
  318.         INT16 *dest1 = buffer[0];
  319.         INT16 *dest2 = buffer[1];
  320.         for (i = 0; i < length; i++)
  321.         {
  322.             /*
  323.             *dest1++ = 8*(*lmix++);
  324.             *dest2++ = 8*(*rmix++);
  325.             */
  326.             *dest1++ = limit(8*(*lmix++));            /* 991112.CAB */
  327.             *dest2++ = limit(8*(*rmix++));
  328.         }
  329.     }
  330. }
  331.  
  332. int C140_sh_start( const struct MachineSound *msound )
  333. {
  334.     const struct C140interface *intf = msound->sound_interface;
  335.     int vol[2];
  336.     const char *stereo_names[2] = { "C140 Left", "C140 Right" };
  337.  
  338.     vol[0] = MIXER(intf->mixing_level,MIXER_PAN_LEFT);
  339.     vol[1] = MIXER(intf->mixing_level,MIXER_PAN_RIGHT);
  340.  
  341.     sample_rate=baserate=intf->frequency;
  342.  
  343.     stream = stream_init_multi(2,stereo_names,vol,sample_rate,0,update_stereo);
  344.  
  345.     pRom=memory_region(intf->region);
  346.  
  347.     memset(REG,0,0x200 );
  348.     {
  349.         int i;
  350.         for(i=0;i<MAX_VOICE;i++) init_voice( &voi[i] );
  351.     }
  352.  
  353.     /* allocate a pair of buffers to mix into - 1 second's worth should be more than enough */
  354.     mixer_buffer_left = malloc(2 * sizeof(short)*sample_rate );
  355.     if( mixer_buffer_left )
  356.     {
  357.         mixer_buffer_right = mixer_buffer_left + sample_rate;
  358.         return 0;
  359.     }
  360.     return 1;
  361. }
  362.  
  363. void C140_sh_stop( void )
  364. {
  365.     free( mixer_buffer_left );
  366. }
  367.  
  368.